<?php
/**
 * 专项基金营销活动
 * @author Administrator
 *
 */
class SpecialOffline extends MY_Controller{
    function SpecialOffline() {
        parent::MY_Controller();
    }
    /**
     * 添加或修改
     */
    function add() {
        $this->load->model('specialOfflineModel');
        if ($_POST) {
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId' => $this->user['userId'],
                'procId' => (int)$this->input->post('procId'),
                'projectName' => $this->input->post('projectName'),
                'mediaType' => (int)$this->input->post('mediaType'),
                'mediaForm' => (int)$this->input->post('mediaForm'),
                'mediaFormAlias' => $this->input->post('mediaFormAlias'),
                'theme' => $this->input->post('theme'),
                'address' => $this->input->post('address'),
                'startTime' => $this->input->post('startTime'),
                'endTime' => $this->input->post('endTime'),
                'targetCustomer' => (int)$this->input->post('targetCustomer'),
                'targetOrder' => (int)$this->input->post('targetOrder'),
                'budgetApply' => (float)$this->input->post('budgetApply'),
                'budgetFileId' => (int)$this->input->post('budgetFileId'),
                'remarks' => $this->input->post('remarks'),
                'voucher' => serialize(array()),
            );
            $data['storeId'] || showError('请选择所属专营店ID');
            $data['procId'] || showError('请选择所属主线流程');
            $data['projectName'] || showError('请输入项目名称');
            $data['mediaType'] || showError('请选择活动类型');
            /* $data['mediaForm'] || showError('请选择活动形式');
            if ($data['mediaForm'] == -1 && !$data['mediaFormAlias']){
                showError('请输入其他活动形式');
            } */
            $data['theme'] || showError('请输入活动主题');
            $data['address'] || showError('请输入活动地点');
            $data['startTime'] || showError('请选择开始时间');
            $data['endTime'] || showError('请选择结束时间');
            strtotime($data['startTime']) <= strtotime($data['endTime']) || showError('开始时间必须早于结束时间');
            $data['targetCustomer'] > 0 || showError('请输入集客目标');
            $data['targetOrder'] > 0 || showError('请输入订单目标');
            $data['budgetApply'] > 0 || showError('请输入预算申请金额');
            $data['budgetFileId'] || showError('请上传营销活动方案');
            if ($id){
                $findData = $this->specialOfflineModel->getData($id);
                if (in_array($findData['budgetAuditRs'], array(0,3))){
                    $data['budgetAuditRole'] = 0;
                    $data['budgetAuditRs'] = 0;
                }
                $id = $this->specialOfflineModel->edit(array('id'=>$id,'storeId'=>$this->user['userId']), $data);
                if ($id){
                    $this->specialOfflineModel->updateMoney($id);
                    showSuccess('编辑成功');
                }else{
                    showError('编辑失败');
                }
            }else{
                $data['createTime'] = time();
                $id = $this->specialOfflineModel->add($data);
                if ($id){
                    $this->specialOfflineModel->updateMoney($id);
                    showSuccess('添加成功');
                }else{
                    showError('添加失败');
                }
            }
        }else{
            $procId = (int)$this->input->get('procId');
            $id = (int)$this->input->get('id');
            if ($id){
                $offlineData = $this->specialOfflineModel->getData($id);
                $this->viewData['offlineData'] = $offlineData;
                $procId = (int)$offlineData['procId'];
            }
            $this->load->model('specialProcessModel');
            $procData = $this->specialProcessModel->getData($procId);
            $procData || showError('主线流程数据不存在');
            $this->viewData['procData'] = $procData;
            $this->load->view('SpecialOffline/add', $this->viewData);
        }
    }
    /**
     * 删除
     */
    function del(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的营销活动');
        $this->load->model('specialOfflineModel');
        if($this->specialOfflineModel->del(array('id'=>$id, 'storeId'=>$this->user['userId']))){
            showSuccess('删除营销活动成功');
        }else{
            showError('删除营销活动失败');
        }
    }
    /**
     * 结算申请
     */
    function settleEdit(){
        $this->load->model('specialOfflineModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $id || showError('请选择要申请结算的营销活动');
            $procId = (int)$this->input->post('procId');
            $procId || showError('请选择主线流程数据');
            $data = array(
                'actualCustomer' => (int)$this->input->post('actualCustomer'),
                'actualOrder' => (int)$this->input->post('actualOrder'),
                'settleApply' => (float)$this->input->post('settleApply'),
                'settleFileId' => (int)$this->input->post('settleFileId'),
                'settleSubTime' => time(),
                'settleAuditRole' => 0,
                'settleAuditRs' => 0,
            );
            $data['actualCustomer'] || showError('请输入集客效果');
            $data['actualOrder'] || showError('请输入订单效果');
            $data['settleApply'] || showError('请输入结算申请费用');
            $data['settleFileId'] || showError('请上传结算材料');
            /*$this->load->model('fileModel');
            $voucherFiles = isset($_POST['voucher']) ? (array)$_POST['voucher'] : array();
            $voucherList = $this->fileModel->getFileDataByViewPaths($voucherFiles);
            count($voucherList) >= 5 || showError('至少上传5张凭证照片');
            $data['voucher'] = serialize(array_values($voucherList));*/
            if ($this->specialOfflineModel->edit(array('id'=>$id,'storeId'=>$this->user['userId']),$data)){
                $this->specialOfflineModel->updateMoney($id);
                //更新主线流程结算审核状态
                $this->load->model('specialProcessModel');
                $this->specialProcessModel->settleUpdateStatus($procId);
                showSuccess('结算申请成功');
            }else{
                showError('结算申请失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            $id || showError('请选择要查看凭证的营销活动');
            $offlineData = $this->specialOfflineModel->getNewData(array('id'=>$id,'storeId'=>$this->user['userId']));
            $this->viewData['offlineData'] = $offlineData;
            $this->load->view('SpecialOffline/settleEdit', $this->viewData);
        }
    }
    /**
     * 广宣传播凭证查看
     */
    function voucherCheck(){
        $id = (int)$this->input->get('id');
        $id || showError('请选择要查看凭证的营销活动');
        $this->load->model('specialOfflineModel');
        $offlineData = $this->specialOfflineModel->getNewData($id);
        $this->viewData['voucher'] = $offlineData['voucher'];
        $this->load->view('common/voucherCheck', $this->viewData);
    }
}